package com.itheim.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheim.mapper.EmpExprMapper;
import com.itheim.mapper.EmpMapper;
import com.itheim.pojo.*;
import com.itheim.service.EmpLogService;
import com.itheim.service.EmpService;
import com.itheim.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;



//    -----------------原始方法----------------------------
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
////        total=empMapper.count();
////        int start=(page-1)*pageSize;
////        List<Emp> rows = empMapper.page(start,pageSize);
////        return new PageResult<Emp>(total,rows);
//    }

//-------------------PageHelper分页插件
    @Override
   public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> emplist= empMapper.page(empQueryParam);
        Page<Emp> p=(Page<Emp>)emplist;
        return new PageResult<>(p.getTotal(),p.getResult());
   }


   @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
       try {
           //1.补全基础属性
           emp.setCreateTime(LocalDateTime.now());
           emp.setUpdateTime(LocalDateTime.now());
           //2.保存员工基本信息

           empMapper.insert(emp);

           //3. 保存员工的工作经历信息 - 批量
           Integer empId = emp.getId();
           List<EmpExpr> exprList = emp.getExprList();
           if(!CollectionUtils.isEmpty(exprList)){
               exprList.forEach(empExpr -> empExpr.setEmpId(empId));
               empExprMapper.insertBatch(exprList);
           }
       } finally {
           EmpLog empLog=new EmpLog(null,LocalDateTime.now(),"新增员工信息"+emp.toString());
           empLogService.insertLog(empLog);
       }


    }

    @Override
    public void delete(Integer[] ids) {
        empMapper.delete(ids);
        empExprMapper.delete(ids);
    }

    @Override
    public Emp getinfo(Integer id) {
        Emp emp = empMapper.getinfo(id);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updatebyId(emp);
        //删除工作经历信息
        empExprMapper.delete(new Integer[]{emp.getId()});
        //更新工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }


    }

    @Override
    public List<Emp> list() {
       return empMapper.list();
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp e=empMapper.selectByUsernameAndPassword(emp);
        if(e!=null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());
            String jwt = JwtUtils.generateJwt(claims);
            return new LoginInfo(e.getId(),e.getUsername(),e.getName(),jwt);
        }
        return null;
    }


}


